package sing.myapp.ui.h5

import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import sing.core.base.BaseActivity
import sing.core.ext.util.showShort
import sing.myapp.R
import sing.myapp.BR
import sing.myapp.router.JumpCenter
import sing.myapp.router.RouterURL
import sing.myapp.databinding.ActivityH5Binding

/**
 * @Description:    H5 的页面
 */
@Route(path = RouterURL.H5_ACTIVITY_WEB_VIEW)
class H5Activity : BaseActivity<H5ViewModel, ActivityH5Binding>(R.layout.activity_h5,BR.h5Vm) {

    override fun initData() {
        super.initData()
        AndroidBug5497Workaround.assistActivity(this)
        initViewPager()// 不会为空，有默认值0
    }

    val list: ArrayList<Fragment> = ArrayList()
    private fun initViewPager() {
        val v = intent.extras
        if (v == null){
            "参数为空".showShort()
            onBackPressed()
            return
        }else{
            val url = v.getString("url","")
            val title = v.getString("title","")
            val showTitle = v.getBoolean("showTitle")
            val showStatus = v.getBoolean("showStatus")
            val showBack = v.getBoolean("showBack")
            val isBlackBack = v.getBoolean("isBlackBack")

            list.add(JumpCenter.getH5Fragment(url,title,showStatus,showBack,showTitle,isBlackBack))

            db.viewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
                override fun getItem(position: Int) = list[position]
                override fun getCount() = list.size
            }
        }
    }

    override fun createObserver() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        list[0].onActivityResult(requestCode, resultCode, data)
    }
}